package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.LabelInfoMapper;
import com.atguigu.lease.web.admin.service.LabelInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "标签管理")
@RestController
@RequestMapping("/admin/label")
public class LabelController {
    @Autowired
    private LabelInfoService labelInfoService;

    @Operation(summary = "（根据类型）查询标签列表")
    @GetMapping("list")
    public Result<List<LabelInfo>> labelList(@RequestParam(required = false) ItemType type) {
        //要求让根据类型查询,那么我们构造出一个条件构造器,用于写条件插入到查询语句中
        LambdaQueryWrapper<LabelInfo> wrapper = new LambdaQueryWrapper<>();
        if (type != null){
            //写wrapper条件
            wrapper.eq(LabelInfo::getType,type.getCode());
        }
        //根据条件查询所有数据
        List<LabelInfo> AllData = labelInfoService.list(wrapper);
        return Result.ok(AllData);
    }

    @Operation(summary = "新增或修改标签信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdateLabel(@RequestBody LabelInfo labelInfo) {
        /*
         * .saveOrUpdate方法就是新增或修改
         * 如果原来有数据那么就是修改，如果没有数据那么就是新增
         */
        boolean result = labelInfoService.saveOrUpdate(labelInfo);
        if (!result){
            System.out.println("保存失败");
        }else{
            System.out.println("保存成功");
        }
        return Result.ok();
    }

    @Operation(summary = "根据id删除标签信息")
    @DeleteMapping("deleteById")
    public Result deleteLabelById(@RequestParam Long id) {
        boolean result = labelInfoService.removeById(id);
        if (!result){
            System.out.println("删除失败");
        }else{
            System.out.println("删除成功");
        }
        return Result.ok();
    }
}
